package com.z.redis.channel.kqueue;

import com.z.redis.channel.LocalChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.kqueue.KQueue;
import io.netty.channel.kqueue.KQueueChannelOption;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.kqueue.KQueueServerSocketChannel;

import java.util.Objects;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class KqueueChannelOption implements LocalChannelOption {
    private final KQueueEventLoopGroup boss; //主Reactor线程组 处理连接请求
    private final KQueueEventLoopGroup workers; //从Reactor线程组 处理I/O事件

    public KqueueChannelOption(KQueueEventLoopGroup boss, KQueueEventLoopGroup workers){
        this.boss = Objects.requireNonNull(boss, "boss group cannot be null");
        this.workers = Objects.requireNonNull(workers, "workers group cannot be null");
    }
    public KqueueChannelOption(){
        //4线程为高并发预留
        this.boss = new KQueueEventLoopGroup(4, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"Server_boss_" + index.getAndIncrement());
            }
        });
        //默认CPU核数*2
        this.workers = new KQueueEventLoopGroup(8, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"Server_workers_" + index.getAndIncrement());
            }
        });
    }
    @Override
    public EventLoopGroup boss() { return this.boss; }
    @Override
    public EventLoopGroup workers() { return this.workers; }
    //通道类型配置
    @Override
    public Class getChannelClass(){
        return KQueueServerSocketChannel.class;
    }
}
